﻿
using Castle.Core.Internal;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.Interface;
using Coldairarrow.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Interface
{
    public class OL_SalDailyInterfaceBusiness2 : Interface_BaseBusiness, IJob_Business, ITransientDependency
    {

        public List<OL_FilterDTO> Filters;
        public int NoQueryNum { get; set; }
        public Base_InterfaceOption queryOption { get; set; }
        public Base_InterfaceOption insertOption { get; set; }
        public Base_InterfaceOption upDateOption { get; set; }
        public Base_InterfaceOption deleteOption { get; set; }
        public OL_SalDailyInterfaceBusiness2()
        {
            this.InterfaceName = "销售日报明细-2";
            //字段的映射关系
            tableField = GetMapField("addRow");
            //索引字段
            Keys = GetKeys("addRow");
            indexFields.Add(Keys.Where(X => X.Type == "K").First().Field);
            //查询过滤条件
            Filters = new List<OL_FilterDTO>();
            

            //数据库连接
            ConString = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            SqlDb = (SqlServerHelper)DbHelperFactory.GetDbHelper(EFCore.Sharding.DatabaseType.SqlServer, ConString);

            //获取此接口的所有API信息
            this.interfaceOption = SqlDb.GetListBySql<Base_InterfaceOption>(@$"SELECT  * FROM  Base_InterfaceOption(NOLOCK) WHERE Name='{InterfaceName}' ");
            queryOption = this.interfaceOption.Where(o => o.OperateType == "getFilterRows").First();//查询API配置
            insertOption = this.interfaceOption.Where(o => o.OperateType == "addRow").First();//新增API配置
            upDateOption = this.interfaceOption.Where(o => o.OperateType == "editRow").First();//更行API配置
            deleteOption = this.interfaceOption.Where(o => o.OperateType == "deleteRow").First();//删除API配置

            //查询执行中的事件
            OnQueryEvent += Interface_Business_OnQueryEvent; ;
            //查询执行前的事件
            BeforeQueryEvent += Interface_Business_BeforeQueryEvent;
            //执行非查询请求中的事件
            OnNoQueryEvent += Interface_Business_OnNoQueryEvent;

        }

        /// <summary>
        /// 执行非查询请求中时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="noQueryEventArgs"></param>
        private void Interface_Business_OnNoQueryEvent(object sender, NoQueryEventArgs noQueryEventArgs)
        {
            //API请求是否成功
            noQueryEventArgs.Success = noQueryEventArgs.Result["success"].ToString().ToBool();
        }


        /// <summary>
        /// 查询执行前的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="queryEventArgs"></param>
        private void Interface_Business_BeforeQueryEvent(object sender, QueryEventArgs queryEventArgs)
        {
            if (queryEventArgs.Page.Filters.IsNullOrEmpty() && !Filters.IsNullOrEmpty())
            {
                queryEventArgs.Page.Filters = Filters;
            }
        }


        /// <summary>
        /// 查询执行中的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="queryEventArgs"></param>
        private void Interface_Business_OnQueryEvent(object sender, QueryEventArgs queryEventArgs)
        {
            //是否查询完成
            if (queryEventArgs.Result["data"]["rows"].ToList().Count == 0)
            {
                queryEventArgs.Page.Complete = true;
            }
            //向总行中添加新的行
            queryEventArgs.Rows.AddRange(queryEventArgs.Result["data"]["rows"].ToList());
            //获取总页数
            queryEventArgs.Page.Total = queryEventArgs.Result["data"]["total"].ToString().ToInt();
            //获取查询成功标志
            queryEventArgs.Success = queryEventArgs.Result["success"].ToString().ToBool();
        }

        /// <summary>
        /// 新增更新表
        /// </summary>
        public async void ExecuteInterface()
        {

            try
            {
                //获取销售目标报表
                OL_SalTargetInterfaceBusiness salTarget = new OL_SalTargetInterfaceBusiness();
                List<SalTarget> salTargets = await salTarget.QueryAsync<SalTarget>(salTarget.queryOption, salTarget.tableField, salTarget.SqlDb);
                if (salTargets.Count > 0 && !salTargets.IsNullOrEmpty())
                {
                    foreach (var item1 in salTargets)
                    {
                        //SKU
                        List<AssTable> table = item1.SKU.ToObject<List<AssTable>>();
                        if (table.Count > 0) item1.SKU_T = table[0];
                        //产品类型
                        table = item1.ProType.ToObject<List<AssTable>>();
                        if (table.Count > 0) item1.ProType_T = table[0];
                        //亚马逊店铺
                        table = item1.Store.ToObject<List<AssTable>>();
                        if (table.Count > 0) item1.Store_T = table[0];
                        //部门责任人
                        List<Choice> choice = item1.DepResponsible.ToObject<List<Choice>>();
                        if (choice.Count > 0) item1.DepResponsible_T = choice[0];
                        //销售人员
                        choice = item1.SalPerson.ToObject<List<Choice>>();
                        if (choice.Count > 0) item1.SalPerson_T = choice[0];
                    }
                }
                //获取亚马逊报表
                OL_AmazonRepInterfaceBusiness amazonRep = new OL_AmazonRepInterfaceBusiness();
                List<AmazonRep> amazonReps = await amazonRep.QueryAsync<AmazonRep>(amazonRep.queryOption, amazonRep.tableField, amazonRep.SqlDb);
                foreach (var item1 in amazonReps)
                {
                    //销售人员
                    List<Choice> choice = item1.SalPerson.ToObject<List<Choice>>();
                    if (choice.Count > 0) item1.SalPerson_T = choice[0];
                    //产品品类
                    List<AssTable> table = item1.ProType.ToObject<List<AssTable>>();
                    if (table.Count > 0) item1.ProType_T = table[0];
                    //SKU
                    table = item1.SKU.ToObject<List<AssTable>>();
                    if (table.Count > 0) item1.SKU_T = table[0];
                    //品牌
                    table = item1.Brand.ToObject<List<AssTable>>();
                    if (table.Count > 0) item1.Brand_T = table[0];
                    //亚马逊店铺
                    table = item1.Store.ToObject<List<AssTable>>();
                    if (table.Count > 0) item1.Store_T = table[0];
                }
                //获取源数据
                List<SalDaily2> sourceData = new List<SalDaily2>();


                #region 生成每一行数据
                if (salTargets.Count > 0 && amazonReps.Count > 0)
                {

                    //默认了目标数据为当月数据
                    foreach (var T in salTargets)
                    {
                        //获取按照SKU当月的所有数据
                        List<AmazonRep> SUK_ThetMonth = amazonReps.Where(x =>
                        {
                            bool isOK = false;
                            if (x.SKU_T.IsNullOrEmpty() || T.SKU_T.IsNullOrEmpty()) isOK = false;
                            else if (x.SKU_T.name == T.SKU_T.name && x.Store_T.name == T.Store_T.name) isOK = true;
                            return isOK;
                        }).ToList();


                        //设置公共数据
                        SalDaily2 salDaily = new SalDaily2();
                        salDaily.DepResponsible = T.DepResponsible_T.IsNullOrEmpty() ? "" : T.DepResponsible_T.accountId;
                        salDaily.DepResponsible_T = T.DepResponsible_T.IsNullOrEmpty() ? "" : T.DepResponsible_T.fullname;
                        salDaily.SalPerson = T.SalPerson_T.IsNullOrEmpty() ? "" : T.SalPerson_T.accountId;
                        salDaily.SalPerson_T = T.SalPerson_T.IsNullOrEmpty() ? "" : T.SalPerson_T.fullname;
                        salDaily.SKU = T.SKU_T.IsNullOrEmpty() ? "" : T.SKU_T.sid;
                        salDaily.SKU_T = T.SKU_T.IsNullOrEmpty() ? "" : T.SKU_T.name;
                        salDaily.Store = T.Store_T.IsNullOrEmpty() ? "" : T.Store_T.sid;
                        salDaily.Store_T = T.Store_T.IsNullOrEmpty() ? "" : T.Store_T.name;
                        salDaily.ProType = T.ProType_T.IsNullOrEmpty() ? "" : T.ProType_T.sid;
                        salDaily.ProType_T = T.ProType_T.IsNullOrEmpty() ? "" : T.ProType_T.name;
                        salDaily.Department = T.Department.IsNullOrEmpty() ? "" : T.Department;
                        salDaily.StartDate = T.StartDate.IsNullOrEmpty() ? "" : T.StartDate;
                        salDaily.EndDate = T.EndDate.IsNullOrEmpty() ? "" : T.EndDate;
                        salDaily.Month = T.StartDate.IsNullOrEmpty() ? "" : T.StartDate.ToDateTime().ToString("yyyy-MM");

                        if (salDaily.SKU_T == "FDG340IN-CS90-NG" || salDaily.SKU_T == "FSETR12" || salDaily.SKU_T == "FDG340IN-CS90-LP")
                        {
                            string wwet = "";
                        }


                        //创建每一行指标的数据
                        List<SalDaily2> salDailies = new List<SalDaily2>();
                        SalDaily2 salDaily1 = salDaily.DeepClone();
                        salDaily1.Indicators = "2.目标销量";
                        salDaily1.IndicatorsId = "001";
                        salDaily1.IndicatorsValue = T.TargetVolume.IsNullOrEmpty() ? "0" : T.TargetVolume;
                        salDailies.Add(salDaily1);

                        SalDaily2 salDaily2 = salDaily.DeepClone();
                        salDaily2.Indicators = "1.月销量";
                        salDaily2.IndicatorsId = "002";
                        if (SUK_ThetMonth.Count > 0 || !SUK_ThetMonth.IsNullOrEmpty())
                        {
                            SUK_ThetMonth.Where(x => x.ThatDayVolume.IsNullOrEmpty()).ForEach(x => x.ThatDayVolume = "0");
                            salDaily2.IndicatorsValue = SUK_ThetMonth.Sum(x => x.ThatDayVolume.ToInt()).ToString();
                        }
                        else
                        {
                            salDaily2.IndicatorsValue = "0";
                        }
                        salDailies.Add(salDaily2);

                        SalDaily2 salDaily3 = salDaily.DeepClone();
                        salDaily3.Indicators = "4.目标销售额";
                        salDaily3.IndicatorsId = "003";
                        salDaily3.IndicatorsValue = T.TargetSales.IsNullOrEmpty() ? "0" : T.TargetSales;
                        salDailies.Add(salDaily3);

                        SalDaily2 salDaily4 = salDaily.DeepClone();
                        salDaily4.Indicators = "3.月销售额";
                        salDaily4.IndicatorsId = "004";
                        if (SUK_ThetMonth.Count > 0 || !SUK_ThetMonth.IsNullOrEmpty())
                        {
                            SUK_ThetMonth.Where(x => x.ThatDaySales.IsNullOrEmpty()).ForEach(x => x.ThatDaySales = "0");
                            salDaily4.IndicatorsValue = SUK_ThetMonth.Sum(x => x.ThatDaySales.ToDouble()).ToString();
                        }
                        else
                        {
                            salDaily4.IndicatorsValue = "0";
                        }
                        salDailies.Add(salDaily4);

                        SalDaily2 salDaily5 = salDaily.DeepClone();
                        salDaily5.Indicators = "5.达成率(%)";
                        salDaily5.IndicatorsId = "005";
                        //如果分母为0结果直接为0
                        if (salDaily3.IndicatorsValue.ToDouble() == 0) salDaily5.IndicatorsValue = "0.00";
                        else salDaily5.IndicatorsValue = (salDaily4.IndicatorsValue.ToDouble() / salDaily3.IndicatorsValue.ToDouble() * 100).ToString("0.00", CultureInfo.InvariantCulture);
                        salDailies.Add(salDaily5);

                        SalDaily2 salDaily6 = salDaily.DeepClone();
                        salDaily6.Indicators = "7.目标广告";
                        salDaily6.IndicatorsId = "006";
                        salDaily6.IndicatorsValue = T.TargetAdv.IsNullOrEmpty() ? "0" : T.TargetAdv;
                        salDailies.Add(salDaily6);

                        SalDaily2 salDaily7 = salDaily.DeepClone();
                        salDaily7.Indicators = "6.月广告";
                        salDaily7.IndicatorsId = "007";
                        if (SUK_ThetMonth.Count > 0 || !SUK_ThetMonth.IsNullOrEmpty())
                        {
                            SUK_ThetMonth.Where(x => x.ThatDayAdv.IsNullOrEmpty()).ForEach(x => x.ThatDayAdv = "0");
                            salDaily7.IndicatorsValue = SUK_ThetMonth.Sum(x => x.ThatDayAdv.ToDouble()).ToString("0.00", CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            salDaily7.IndicatorsValue = "0";
                        }
                        salDailies.Add(salDaily7);

                        SalDaily2 salDaily8 = salDaily.DeepClone();
                        salDaily8.Indicators = "9.目标ACOS(%)";
                        salDaily8.IndicatorsId = "008";
                        salDaily8.IndicatorsValue = T.TargetACOS.IsNullOrEmpty() ? "0" : T.TargetACOS;
                        salDailies.Add(salDaily8);

                        SalDaily2 salDaily9 = salDaily.DeepClone();
                        salDaily9.Indicators = "8.月ACOS(%)";
                        salDaily9.IndicatorsId = "009";
                        if (salDaily7.IndicatorsValue.ToDouble() != 0 && salDaily4.IndicatorsValue.ToDouble() != 0)
                        {
                            //SUK_ThetMonth.Where(x => x.ThatDayACOS.IsNullOrEmpty()).ForEach(x => x.ThatDayACOS = "0");
                            //salDaily9.IndicatorsValue = SUK_ThetMonth.Sum(x => x.ThatDayACOS.ToDouble()).ToString("0.00", CultureInfo.InvariantCulture);
                            salDaily9.IndicatorsValue = (salDaily7.IndicatorsValue.ToDouble() / salDaily4.IndicatorsValue.ToDouble() * 100).ToString("0.00", CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            salDaily9.IndicatorsValue = "0";
                        }
                        salDailies.Add(salDaily9);

                        //设置每一行每一天的数据
                        SetDayValue(salDailies, amazonReps, T);


                        sourceData.AddRange(salDailies);
                    }


                }
                #endregion

                //设置ID
                sourceData.ForEach(x => x.ID = x.SKU_T + x.Store_T + x.IndicatorsId);
                var t = sourceData.GroupBy(x => x.ID).Where(x => x.Count() > 1);

                //获取目标数据
                List<SalDaily2> targetData = new List<SalDaily2>();//目标据集合
                Filters.Clear();
                Filters.Add(
                new OL_FilterDTO() { ControlId = "", DataType = 15, SpliceType = 1, FilterType = 14, Value = "" }
            );
                targetData = await QueryAsync<SalDaily2>(queryOption, tableField, SqlDb);
                foreach (var item1 in targetData)
                {
                    //销售人员
                    List<Choice> choice = new List<Choice>();
                    if (!item1.SalPerson.IsNullOrEmpty()) choice = item1.SalPerson.ToObject<List<Choice>>();
                    if (choice.Count > 0 && !choice.IsNullOrEmpty()) item1.SalPerson = choice[0].accountId;
                    //部门管理
                    if (!item1.DepResponsible.IsNullOrEmpty()) choice = item1.DepResponsible.ToObject<List<Choice>>();
                    if (choice.Count > 0 && !choice.IsNullOrEmpty()) item1.DepResponsible = choice[0].accountId;
                    //产品品类
                    List<AssTable> table = new List<AssTable>();
                    if (!item1.ProType.IsNullOrEmpty()) table = item1.ProType.ToObject<List<AssTable>>();
                    if (table.Count > 0 && !choice.IsNullOrEmpty()) item1.ProType = table[0].sid;
                    //SKU
                    if (!item1.SKU.IsNullOrEmpty()) table = item1.SKU.ToObject<List<AssTable>>();
                    if (table.Count > 0 && !choice.IsNullOrEmpty()) item1.SKU = table[0].sid;
                    //亚马逊店铺
                    if (!item1.Store.IsNullOrEmpty()) table = item1.Store.ToObject<List<AssTable>>();
                    if (table.Count > 0 && !choice.IsNullOrEmpty()) item1.Store = table[0].sid;
                }

                if (targetData.Count > 0 && sourceData.Count > 0)
                {
                    //更新
                    #region
                    List<SalDaily2> updataItem = sourceData.Intersect(targetData, new ComparerFromKeys<SalDaily2>(Keys, "RowId")).ToList();//获取交集(更新项)
                    List<OL_RowDTO> updataData = ConvertOL_Row(updataItem, tableField);//需要更新的数据集合
                    ExecuteNoQuery<OL_RowDTO>(updataData, upDateOption, SqlDb);//执行更新
                    #endregion

                    //新增
                    #region
                    List<SalDaily2> insertItem = sourceData.Except(targetData, new ComparerFromKey<SalDaily2>(indexFields.First(), "RowId")).ToList();//获取源数据对目标数据的差集(新增项)
                    List<OL_RowDTO> insertData = ConvertOL_Row(insertItem, tableField);//需要新增的数据集合
                    ExecuteNoQuery<OL_RowDTO>(insertData, insertOption, SqlDb);//执行插入行
                    #endregion

                    //删除
                    #region
                    List<SalDaily2> deleteItem = targetData.Except(sourceData, new ComparerFromKey<SalDaily2>(indexFields.First(), "RowId")).ToList();//获取目标数对据源数据的差集(删除项)
                    //获取有重复的项
                    List<SalDaily2> repeatItem = targetData.GroupBy(x => x.GetPropertyValue(indexFields.First())).Where(x => x.Count() > 1).Select(x => x.FirstOrDefault()).ToList();
                    //将重复的项也一起加入删除的列表
                    deleteItem.AddRange(repeatItem);
                    //筛选出删除列表中重复的项
                    var temp = deleteItem.GroupBy(x => x.GetPropertyValue(indexFields.First())).Where(x => x.Count() > 1).Select(x => x.FirstOrDefault()).ToList();
                    deleteItem.AddRange(temp);
                    List<OL_RowDTO> deleteData = ConvertOL_Row(deleteItem, tableField);//需要删除的数据集合
                    ExecuteNoQuery<OL_RowDTO>(deleteData, deleteOption, SqlDb);//执行删除行
                    #endregion
                }
                else
                {
                    throw new NotImplementedException("目标/源查询无数据无法执行接口!!");

                }
            }
            catch (Exception ex)
            {
                AddLog(SqlDb, ex.Message, InterfaceName, "erro", "");
            }
        }

        /// <summary>
        /// 设置销售明细的列数据
        /// </summary>
        /// <param name="salDailies">需要新增的行</param>
        /// <param name="amazonReps">SKU每日销售数据</param>
        /// <param name="salTarget">SKU的目标数据</param>
        /// <returns></returns>
        public bool SetDayValue(List<SalDaily2> salDailies, List<AmazonRep> amazonReps, SalTarget salTarget)
        {
            bool isOK = false;
            try
            {
                if (salDailies.Count > 0 && amazonReps.Count > 0 && !salTarget.IsNullOrEmpty())
                {
                    //循环1-31日每天
                    for (int i = 1; i < 32; i++)
                    {
                        //获取SKU及每日的数据
                        List<AmazonRep> data = amazonReps.Where(x =>
                        {
                            bool isOK = false;
                            if (x.SKU_T.IsNullOrEmpty() || x.Store_T.IsNullOrEmpty()) isOK = false;
                            else if (x.SKU_T.sid == salDailies[0].SKU && x.Store_T.sid == salDailies[0].Store && x.Date.ToDateTime().ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM") + "-" + i.ToString("D2")) isOK = true;
                            return isOK;
                        }).ToList();

                        if (!data.IsNullOrEmpty() && data.Count > 0)
                        {
                            //循环设置每一行的每一列(1-31日)的数据
                            salDailies.ForEach(x =>
                            {
                                //目标销量
                                if (x.IndicatorsId == "001")
                                {
                                    //设置每一天的数据
                                    if (!salTarget.TargetVolume.IsNullOrEmpty()) x.SetPropertyValue("Date" + i.ToString(), salTarget.TargetVolume);
                                    else x.SetPropertyValue("Date" + i.ToString(), "0");
                                }
                                //实际月销量
                                else if (x.IndicatorsId == "002")
                                {
                                    //设置每一天的数据
                                    if (data.Count > 0 && !data.IsNullOrEmpty())
                                    {
                                        int sum = data.Sum(x => x.ThatDayVolume.ToInt());
                                        x.SetPropertyValue("Date" + i.ToString(), sum.ToString());
                                    }
                                    else
                                    {
                                        x.SetPropertyValue("Date" + i.ToString(), "0");
                                    }
                                }
                                //目标销售额
                                else if (x.IndicatorsId == "003")
                                {
                                    //设置每一天的数据
                                    if (!salTarget.TargetSales.IsNullOrEmpty()) x.SetPropertyValue("Date" + i.ToString(), salTarget.TargetSales);
                                    else x.SetPropertyValue("Date" + i.ToString(), "0");

                                }
                                //实际月销售额
                                else if (x.IndicatorsId == "004")
                                {
                                    //设置每一天的数据
                                    if (data.Count > 0 && !data.IsNullOrEmpty())
                                    {
                                        double sum = data.Sum(x => x.ThatDaySales.ToDouble());
                                        x.SetPropertyValue("Date" + i.ToString(), sum.ToString());
                                    }
                                    else
                                    {
                                        x.SetPropertyValue("Date" + i.ToString(), "0");
                                    }
                                }
                                //实际达成率
                                else if (x.IndicatorsId == "005")
                                {
                                    //获取目标销售额
                                    double tar = salDailies.Where(y => y.IndicatorsId == "003").First().GetPropertyValue("Date" + i.ToString()).ToString().ToDouble();
                                    //获取实际销售额
                                    double rel = salDailies.Where(y => y.IndicatorsId == "004").First().GetPropertyValue("Date" + i.ToString()).ToString().ToDouble();
                                    if (tar > 0 && rel > 0)
                                    {
                                        x.SetPropertyValue("Date" + i.ToString(), (rel / tar * 100).ToString("0.00", CultureInfo.InvariantCulture));
                                    }
                                    else
                                    {
                                        x.SetPropertyValue("Date" + i.ToString(), "0");
                                    }
                                }
                                //目标广告
                                else if (x.IndicatorsId == "006")
                                {
                                    //设置每一天的数据
                                    if (!salTarget.TargetAdv.IsNullOrEmpty()) x.SetPropertyValue("Date" + i.ToString(), salTarget.TargetAdv);
                                    else x.SetPropertyValue("Date" + i.ToString(), "0");
                                }
                                //实际月广告
                                else if (x.IndicatorsId == "007")
                                {
                                    //设置每一天的数据
                                    if (data.Count > 0 && !data.IsNullOrEmpty())
                                    {
                                        double sum = data.Sum(x => x.ThatDayAdv.ToDouble());
                                        x.SetPropertyValue("Date" + i.ToString(), sum.ToString());
                                    }
                                    else
                                    {
                                        x.SetPropertyValue("Date" + i.ToString(), "0");
                                    }
                                }
                                //目标ACOS
                                else if (x.IndicatorsId == "008")
                                {
                                    //设置每一天的数据
                                    if (!salTarget.TargetACOS.IsNullOrEmpty()) x.SetPropertyValue("Date" + i.ToString(), salTarget.TargetACOS);
                                    else x.SetPropertyValue("Date" + i.ToString(), "0");
                                }
                                //实际月ACOS
                                else if (x.IndicatorsId == "009")
                                {
                                    //设置每一天的数据
                                    //double sum = data.Sum(x => x.ThatDayACOS.ToDouble());
                                    //x.SetPropertyValue("Date" + i.ToString(), sum.ToString());

                                    //获取实际广告费
                                    double adv = salDailies.Where(y => y.IndicatorsId == "007").First().GetPropertyValue("Date" + i.ToString()).ToString().ToDouble();
                                    //获取实际销售额
                                    double sal = salDailies.Where(y => y.IndicatorsId == "004").First().GetPropertyValue("Date" + i.ToString()).ToString().ToDouble();
                                    if (adv > 0 && sal > 0)
                                    {
                                        x.SetPropertyValue("Date" + i.ToString(), (adv / sal * 100).ToString("0.00", CultureInfo.InvariantCulture));
                                    }
                                    else
                                    {
                                        x.SetPropertyValue("Date" + i.ToString(), "0");
                                    }
                                }
                            });
                        }
                        else
                        {
                            salDailies.ForEach(x =>
                            {
                                //目标销量
                                if (x.IndicatorsId == "001")
                                {
                                    //设置每一天的数据
                                    x.SetPropertyValue("Date" + i.ToString(), salTarget.TargetVolume);

                                }
                                //实际月销量
                                else if (x.IndicatorsId == "002")
                                {
                                    //设置每一天的数据
                                    x.SetPropertyValue("Date" + i.ToString(), "0");
                                }
                                //目标销售额
                                else if (x.IndicatorsId == "003")
                                {
                                    //设置每一天的数据
                                    x.SetPropertyValue("Date" + i.ToString(), salTarget.TargetSales);

                                }
                                //实际月销售额
                                else if (x.IndicatorsId == "004")
                                {
                                    //设置每一天的数据
                                    x.SetPropertyValue("Date" + i.ToString(), "0");
                                }
                                //实际达成率
                                else if (x.IndicatorsId == "005")
                                {
                                    x.SetPropertyValue("Date" + i.ToString(), "0");
                                }
                                //目标广告
                                else if (x.IndicatorsId == "006")
                                {
                                    //设置每一天的数据
                                    x.SetPropertyValue("Date" + i.ToString(), salTarget.TargetAdv);

                                }
                                //实际月广告
                                else if (x.IndicatorsId == "007")
                                {
                                    //设置每一天的数据
                                    x.SetPropertyValue("Date" + i.ToString(), "0");

                                }
                                //目标ACOS
                                else if (x.IndicatorsId == "008")
                                {
                                    //设置每一天的数据
                                    x.SetPropertyValue("Date" + i.ToString(), salTarget.TargetACOS);

                                }
                                //实际月ACOS
                                else if (x.IndicatorsId == "009")
                                {
                                    x.SetPropertyValue("Date" + i.ToString(), "0");
                                }
                            });
                        }
                    }

                    return isOK = true;
                }
            }
            catch (Exception ex)
            {
                AddLog(SqlDb, ex.Message, InterfaceName, "erro", "");
            }
            return isOK;
        }





        /// <summary>
        /// 将源数据根据字典映射关系转换成OL_RowDTO
        /// </summary>
        /// <typeparam name="T">源数据</typeparam>
        /// <param name="sourceData">源数据</param>
        /// <param name="map">Key为目标字段名称，Value为源字段名称</param>
        /// <returns></returns>
        public List<OL_RowDTO> ConvertOL_Row<T>(List<T> sourceData, Dictionary<string, string> map)
        {
            List<OL_RowDTO> list = new List<OL_RowDTO>();
            object rowId;
            foreach (var item in sourceData)
            {
                rowId = item.GetPropertyValue("RowId");
                //需要新增的数据
                OL_RowDTO row = new OL_RowDTO() { RowId = rowId.IsNullOrEmpty() ? null : rowId.ToString(), Controls = new List<Control>() };
                foreach (var c in map)
                {
                    //判断对象的字段是否存在
                    if (item.ContainsProperty(c.Value))
                    {
                        //存在则添加数据
                        row.Controls.Add(new Control() { ControlId = c.Key, Value = item.GetPropertyValue(c.Value) });
                    }
                }
                list.Add(row);
            }
            return list;
        }

        /// <summary>
        /// 任务
        /// </summary>
        public void Job()
        {
            ExecuteInterface();
        }
    }



}
